//
//  UpDataViewController.m
//  1243
//
//  Created by 我的 on 16/5/5.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "UpDataViewController.h"
#import "GPDateView.h"
#import "ReadLoginData.h"
#import "NetWorkTools.h"

@interface UpDataViewController ()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *PhoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *AddressTextField;
@property (weak, nonatomic) IBOutlet UITextField *AddressNumberTextField;
@property (weak, nonatomic) IBOutlet UILabel *shengFenLabel;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UILabel *quLabel;

@end

#define DeviceHeight            [[UIScreen mainScreen] bounds].size.height
#define DeviceWidth             [[UIScreen mainScreen] bounds].size.width
#define Key_DistrictSelectProvince          @"DistrictSelectProvince"
#define Key_DistrictSelectProvinceCode      @"DistrictSelectProvinceCode"
#define Key_DistrictSelectProvinceSubCode   @"DistrictSelectProvinceSubCode"
#define Key_DistrictSelectProvinceSub       @"DistrictSelectProvinceSub"
#define Key_DistrictSelectCityCode          @"DistrictSelectCityCode"
#define Key_DistrictSelectCity              @"DistrictSelectCity"

@implementation UpDataViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setUpAddressText];
    
    self.title = @"修改地址";
    
    NSLog(@"%@",self.addressDict);
    
}

/*
 AddTime = "2016-05-09T11:17:29";
 Address = "\U6362\U4e2a\U65b9\U6cd5454";
 City = "\U5ef6\U8fb9\U671d\U9c9c\U65cf\U81ea\U6cbb\U5dde";
 ContactMobile = 15434;
 ContactName = test2;
 ContactPhone = 15434;
 ContactPostCode = 155636845454;
 Countory = "\U6566\U5316\U5e02";
 Id = 47;
 IsDefault = 0;
 Province = "\U5409\U6797\U7701";
 UserId = 1002;
 },
 
 {
 AddTime = "2016-05-06T16:00:06.967";
 Address = ererere;
 City = null;
 ContactMobile = erererw;
 ContactName = dferewr;
 ContactPhone = erererw;
 ContactPostCode = rerere;
 Countory = "";
 Id = 50;
 IsDefault = 0;
 Province = "<null>";
 UserId = 1002;
 },
 
 */

- (void)setUpAddressText {
    
    self.nameTextField.text = [self.addressDict objectForKey:@"ContactName"];
    self.PhoneTextField.text = [self.addressDict objectForKey:@"ContactPhone"];
    self.AddressTextField.text = [self.addressDict objectForKey:@"Address"];
    self.AddressNumberTextField.text = [self.addressDict objectForKey:@"ContactPostCode"];
    
    
    //判断是否为空
    if ([self.addressDict[@"Province"] isEqual:[NSNull null]]) {
        self.shengFenLabel.text = @"空";
    }else{
        self.shengFenLabel.text = [self.addressDict objectForKey:@"Province"];
    }
    
    if ([self.addressDict[@"City"] isEqual:[NSNull null]]) {
        self.cityLabel.text = @"空";
    }else{
        self.cityLabel.text = [self.addressDict objectForKey:@"City"];
    
    }
    
    if ([self.addressDict[@"Countory"] isEqual:[NSNull null]]) {
        self.quLabel.text = @"空";
    }else {
        self.quLabel.text = [self.addressDict objectForKey:@"Countory"];

        
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self registerTextField];
}

- (void)registerTextField {
    [self.nameTextField resignFirstResponder];
    [self.PhoneTextField resignFirstResponder];
    [self.AddressTextField resignFirstResponder];
    [self.AddressNumberTextField resignFirstResponder];
}

- (IBAction)DidClickChooseAddressButton:(id)sender {
    [self pickerView];
}

- (void)pickerView {
    
    GPDateView * dateView = [[GPDateView alloc] initWithFrame:CGRectMake(0, DeviceHeight-250, DeviceWidth, 250) Data:nil];
    
    [dateView showPickerView];
    
    dateView.ActionDistrictViewSelectBlock = ^(NSString *desStr,NSDictionary *selectDistrictDict){
        self.shengFenLabel.text = [selectDistrictDict objectForKey:Key_DistrictSelectProvince];
        self.cityLabel.text = [selectDistrictDict objectForKey:Key_DistrictSelectCity];
        self.quLabel.text = [selectDistrictDict objectForKey:Key_DistrictSelectProvinceSub];
    };
}

- (IBAction)DidClickUpDataAddressButton:(id)sender {
    
    NSDictionary *userData = [ReadLoginData readLoginData];
    
    NSString *userId = [userData objectForKey:@"Number"];
    
    NSDictionary *params = @{
                             @"Id":[self.addressDict objectForKey:@"Id"],
                             @"UserId":userId,
                             @"Province":self.shengFenLabel.text,
                             @"City":self.cityLabel.text,
                             @"Countory":self.quLabel.text,
                             @"Address":self.AddressTextField.text,
                             @"ContactName":self.nameTextField.text,
                             @"ContactPhone":self.PhoneTextField.text,
                             @"ContactMobile":self.PhoneTextField.text,
                             @"ContactPostCode":self.AddressNumberTextField.text,
                             @"AddTime":@"",
                             @"IsDefault":@"0"
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"UPDATEEXPRESS" completeBlock:^(id object) {
       
        NSInteger Id = [[object objectForKey:@"Id"] integerValue];
        
        if (Id == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改地址成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 10010;
            
            [alert show];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改地址失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
        }
    }];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 10010) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
